package cup.cqzj.auth.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cup.core.tool.utils.Func;
import cup.modules.system.entity.*;
import cup.modules.system.service.IDeptService;
import cup.modules.system.service.IPostService;
import cup.modules.system.service.IRegionService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : ruoyu
 * @date : 2022/8/30
 */
@Component
@AllArgsConstructor
@Slf4j
public class TokenGranterUtil {
	private final IDeptService iDeptService;
	private final IPostService iPostService;

	private final IRegionService iRegionService;


	public void generateDeptAndPostName(UserInfo userInfo){
		User user = userInfo.getUser();
		List<Dept> depts = iDeptService.listByIds(Func.toLongList(user.getDeptId()));
		if(!CollectionUtils.isEmpty(depts)){
			userInfo.setDeptName(depts.stream().map(Dept::getDeptName).collect(Collectors.joining(",")));
			List<Region> regions = iRegionService.getBaseMapper().selectList(new QueryWrapper<Region>().in("code", depts
				.stream().map(Dept::getRegionId).collect(Collectors.toList())));
			if (!CollectionUtils.isEmpty(regions)){
				userInfo.setRegionName(regions.stream().map(Region::getProvinceName).collect(Collectors.joining(",")));
			}
		}
		List<Post> posts = iPostService.listByIds(Func.toLongList(user.getPostId()));
		if(!CollectionUtils.isEmpty(posts)){
			userInfo.setPostName(posts.stream().map(Post::getPostName).collect(Collectors.joining(",")));
		}
	}
}
